﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.DTO;

namespace BestBuy.PageGuest
{
    public partial class ListProductOfCategory : System.Web.UI.Page
    {
        CProduct _cbtm = new CProduct();
        CDanhMuc _cdm = new CDanhMuc();
        DanhMuc danhmuc = new DanhMuc();
        private static int _currentPage = 1;
        private static int _currentCountProduct = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            pnEmtry.Visible = false;
            if (!IsPostBack) {
                Filter();
            }
        }

        private void LoadGV(int iddanhmuc)
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            int size = gvListProducts.PageSize;
            int begin = (_currentPage - 1) * size;
            List<SanPham> list = _cbtm.GetListOfIDCatalogy(iddanhmuc, begin, size);
            if (list.Count > 0)
            {
                gvListProducts.DataSource = list;
                gvListProducts.DataBind();
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;
            }
            else
            {
                
                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gvListProducts.DataSource = null;
                gvListProducts.DataBind();
                lbGridViewEmtry.Text = "There are no data.";

            }
        }

        private void Filter()
        {
            _currentPage = 1;
            LoadTotalPageNumber(danhmuc.IDDanhMuc);
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void LoadTotalPageNumber(int status)
        {
            int soLuongTrang = _cbtm.slTrangDuLieuOfIDCatalogy(gvListProducts.PageSize, _currentCountProduct);
            lbTrang.Text = soLuongTrang.ToString();
        }

        private void HyperLinkQuick(int id) {
            DanhMuc dm = new DanhMuc();
            dm = _cdm.GetByID(id);
            if (dm.IDDanhMucCha != null)
            {
                DanhMuc dmparent = new DanhMuc();
                dmparent = _cdm.GetByID((int)dm.IDDanhMucCha);
                HyperLink hl = new HyperLink();
                hl.ID = "hl" + id.ToString();
                if (dmparent.IDDanhMucCha != null)
                {
                    hl.NavigateUrl = "~/PageGuest/ListProductOfCategory.aspx?pid=" + dm.IDDanhMucCha;
                }
                else {
                    hl.NavigateUrl = "~/PageGuest/ListOfCategoryLevel1.aspx?pid=" + dm.IDDanhMucCha;
                }
                hl.Text = dmparent.TenDanhMuc;
                HyperLinkQuick((int)dm.IDDanhMucCha);
                linkQuick.Controls.Add(hl);
                Label lb = new Label();
                lb.Text = " > ";
                lb.ID = "lbb" + dm.IDDanhMuc.ToString();
                linkQuick.Controls.Add(lb);
            } 
        }

        private void LoadDuLieu() {
            if (Request.Params["pid"] != null)
            {
                try {
                    int id = Convert.ToInt32(Request.Params["pid"].ToString());
                    danhmuc = _cdm.GetByID(id);
                    
                    HyperLinkQuick(id);
                    Label lb = new Label();
                    lb.ID = "lb" + id.ToString();
                    _currentCountProduct = _cdm.GetQuantityOfCategoryProduct(id) + danhmuc.SanPhams.Count;
                    lb.Text = danhmuc.TenDanhMuc + " ( " + _currentCountProduct.ToString() + " Products Found)";
                    linkQuick.Controls.Add(lb);
                    //load danh muc con neu co
                    List<DanhMuc> list = new List<DanhMuc>();
                    list = _cdm.GetChildrenOfCategory(danhmuc.IDDanhMuc, 1);
                    dlCatalogyChildren.DataSource = list;
                    dlCatalogyChildren.DataBind();

                }
                catch {
                    Response.Redirect("~/PageGuest/PageError.aspx");
                }
            }
            else {
                Response.Redirect("~/PageGuest/PageError.aspx");
            }
        }

        public string LoadTen() {
            return danhmuc.TenDanhMuc;
        }

        public string LoadCount() {
            return "( " + _currentCountProduct.ToString() + " Products )";
        }

        public string LoadTotalPage() {
            return " "+lbTrang.Text+" ";
        }

        public string LoadCurrentPage() {
            return " " + _currentPage.ToString()+" ";
        }

        public string CheckHinhAnh(object obj)
        {
            string strHinh = "";
            if (obj != null && !obj.ToString().Equals(""))
            {
                strHinh = "~/Images/Product/" + obj.ToString();
            }
            else {
                strHinh = "~/Images/Product/noimageavallable.jpg";
            }
            return strHinh;
        }

        public string LoadChildrenCount(object obj) {
            int ID = Convert.ToInt32(obj.ToString());
            DanhMuc dm = _cdm.GetByID(ID);
            int count = _cdm.GetQuantityOfCategoryProduct(ID) + dm.SanPhams.Count;
            return count.ToString();
        }

        public string LoadPhuongThucThanhToan(object obj) {
            string str = "";
            if (obj != null) {
                String[] strmang = obj.ToString().Split('+');
                if (strmang[0] == "1")
                {
                    str += "T/T, ";
                }
                if (strmang[1] == "1")
                {
                    str += "L/C, ";
                }
                if (strmang[2] == "1")
                {
                    str += "Western Union, ";
                }
                if (strmang[3] == "1")
                {
                    str += "Paypal, ";
                }
            }
            return str;
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                LoadGV(danhmuc.IDDanhMuc);
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                LoadGV(danhmuc.IDDanhMuc);
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void btnHeaderContact_Click(object sender, EventArgs e)
        {
            int size = gvListProducts.Rows.Count;
            CListProduct.list = new List<SanPham>() ;
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gvListProducts.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        SanPham sp = new SanPham();
                        sp.IDSanPham = Convert.ToInt32(gvListProducts.DataKeys[i].Values[0].ToString());
                        sp.TenSanPham = gvListProducts.DataKeys[i].Values[1].ToString();
                        sp.IDTaiKhoan = Convert.ToInt32(gvListProducts.DataKeys[i].Values[2].ToString());
                        CListProduct.list.Add(sp); 
                    }
                }
                catch
                {
                }
            }
            //chuyen sang trang contact voi doi tuong pid=0 -> danh cho nhiu doi tuong
            Response.Redirect("~/PageGuest/PageContactMessage.aspx?ml=1&pid=0");
        }
    }
}